//
//  MDHAddressPickerView.swift
//  MDHPT
//
//  Created by MengYou on 2019/8/9.
//  Copyright © 2019年 高得华. All rights reserved.
//

import UIKit

///枚举
@objc enum MDHAddressPickerViewType: Int {
    //只显示省
    case province = 1
    //显示省份和城市
    case city     = 2
    //显示省市区，默认
    case area     = 3
    //显示省市区镇，默认
    case town     = 4
}


///本地的城市数据
fileprivate var localPcaDatas = [MDHAddressPickerViewModel]()

/// 选择地址
open class MDHAddressPickerView: UIButton {
    ///block回调
    ///省份的回调
    fileprivate var provinceBlock: ((_ province: MDHAddressPickerViewModel)->())?
    ///城市的回调
    fileprivate var cityBlock: ((_ province: MDHAddressPickerViewModel, _ city: MDHAddressPickerViewModel)->())?
    ///区域的回调
    fileprivate var areaBlock: ((_ province: MDHAddressPickerViewModel, _ city: MDHAddressPickerViewModel, _ area: MDHAddressPickerViewModel)->())?
    ///城镇的回调
    fileprivate var townBlock: ((_ province: MDHAddressPickerViewModel, _ city: MDHAddressPickerViewModel, _ area: MDHAddressPickerViewModel, _ town: MDHAddressPickerViewModel)->())?
    ///有几栏
    fileprivate var columnCount = 0
    //显示类型
    fileprivate var showType = MDHAddressPickerViewType.area {
        didSet {
            self.columnCount = showType.rawValue
            //self.pickerView?.reloadAllComponents()
        }
    }
    
    ///当前选中的 省 Model
    fileprivate var selectProvinceModel = MDHAddressPickerViewModel()
    ///传进来的默认选中的市 Model
    fileprivate var selectCityModel = MDHAddressPickerViewModel()
    ///传进来的默认选中的区 Model
    fileprivate var selectAreaModel = MDHAddressPickerViewModel()
    ///传进来的默认选中的镇 Model
    fileprivate var selectTownModel = MDHAddressPickerViewModel()
    
    ///数据源
    fileprivate var dataModels = [MDHAddressPickerViewModel]()
    
    ///pickerView
    fileprivate weak var pickerView: UIPickerView?
    ///内容视图
    fileprivate weak var viewContain: UIView?
    ///title 标题
    @objc open lazy var labelTitle: UILabel = {
        let label = UILabel()
        label.textAlignment = .center
        label.textColor = UIColor.black
        label.font = UIFont.boldSystemFont(ofSize: 14)
        label.text = "选择地点"
        return label
    }()
    
    ///创建视图
    ///方法一：1.只显示省份一级 2.传入了省份自动滚动到省份，没有传或者找不到默认选中第一个 3.回调省份
    @objc public class func provincePickerViewWith(province: String = "", blockProvince: ((_ province: String)->())?) -> MDHAddressPickerView {
        return MDHAddressPickerView.addressPickerViewWith(province: province, style: .province, blockProvince: blockProvince, blockCity: nil, blockArea: nil, blockTown: nil)
    }
    ///方法二：1.显示省份和市级 2.传入了省份和城市自动滚动到选中的，没有传或者找不到默认选中第一个 3.回调省份和城市
    @objc public class func cityPickerViewWith(province: String = "", city: String = "", blockCity: ((_ province: String, _ city: String)->())?) -> MDHAddressPickerView {
        return MDHAddressPickerView.addressPickerViewWith(province: province, city: city, style: .city, blockProvince: nil, blockCity: blockCity, blockArea: nil, blockTown: nil)
    }
    ///方法三：1.显示省份和市级和区域 2.传入了省份和城市和区域自动滚动到选中的，没有传或者找不到默认选中第一个 3.回调省份城市和区域
    @objc public class func areaPickerViewWith(province: String = "", city: String = "", area: String = "", blockArea: ((_ province: String, _ city: String, _ area: String)->())?) -> MDHAddressPickerView {
        return MDHAddressPickerView.addressPickerViewWith(province: province, city: city, area: area, style: .area, blockProvince: nil, blockCity: nil, blockArea: blockArea, blockTown: nil)
    }
    ///方法四：1.显示省份和市级和区域 镇 2.传入了省份和城市和区域自动滚动到选中的，没有传或者找不到默认选中第一个 3.回调省份城市和区域
    @objc public class func townPickerViewWith(province: String = "", city: String = "", area: String = "", town: String = "", blockTown: ((_ province: String, _ city: String, _ area: String, _ town: String)->())?) -> MDHAddressPickerView {
        return MDHAddressPickerView.addressPickerViewWith(province: province, city: city, area: area, town: town, style: .town, blockProvince: nil, blockCity: nil, blockArea: nil, blockTown: blockTown)
    }
    ///私有方法 创建视图
    fileprivate class func addressPickerViewWith(province: String = "", city: String = "", area: String = "", town: String = "", style: MDHAddressPickerViewType = .area, blockProvince: ((_ province: String)->())?, blockCity: ((_ province: String, _ city: String)->())?, blockArea: ((_ province: String, _ city: String, _ area: String)->())?, blockTown: ((_ province: String, _ city: String, _ area: String, _ town: String)->())?) -> MDHAddressPickerView {
        ///
        let provinceM = MDHAddressPickerViewModel()
        provinceM.name = province
        ///
        let cityM = MDHAddressPickerViewModel()
        cityM.name = city
        ///
        let areaM = MDHAddressPickerViewModel()
        areaM.name = area
        ///
        let townM = MDHAddressPickerViewModel()
        townM.name = town
        
        return MDHAddressPickerView.addressModelPickerViewWith(province: provinceM, city: cityM, area: areaM, town: townM, style: style, blockProvince: { (pm) in
            if blockProvince != nil {
                blockProvince!(pm.name)
            }
        }, blockCity: { (pm, cm) in
            if blockCity != nil {
                blockCity!(pm.name, cm.name)
            }
        }, blockArea: { (pm, cm, am) in
            if blockArea != nil {
                blockArea!(pm.name, cm.name, am.name)
            }
        }, blockTown: { (pm, cm, am, tm) in
            if blockTown != nil {
                blockTown!(pm.name, cm.name, am.name, tm.name)
            }
        })
    }
    
    ///方法一Model：1.只显示省份一级 2.传入了省份自动滚动到省份，没有传或者找不到默认选中第一个 3.回调省份
    @objc public class func provinceModelPickerViewWith(datas: [MDHAddressPickerViewModel] = [MDHAddressPickerViewModel](), province: MDHAddressPickerViewModel = MDHAddressPickerViewModel(), blockProvince: ((_ province: MDHAddressPickerViewModel)->())?) -> MDHAddressPickerView {
        return MDHAddressPickerView.addressModelPickerViewWith(datas: datas, province: province, style: .province, blockProvince: blockProvince, blockCity: nil, blockArea: nil, blockTown: nil)
    }
    ///方法二Model：1.显示省份和市级 2.传入了省份和城市自动滚动到选中的，没有传或者找不到默认选中第一个 3.回调省份和城市
    @objc public class func cityModelPickerViewWith(datas: [MDHAddressPickerViewModel] = [MDHAddressPickerViewModel](), province: MDHAddressPickerViewModel = MDHAddressPickerViewModel(), city: MDHAddressPickerViewModel = MDHAddressPickerViewModel(), blockCity: ((_ province: MDHAddressPickerViewModel, _ city: MDHAddressPickerViewModel)->())?) -> MDHAddressPickerView {
        return MDHAddressPickerView.addressModelPickerViewWith(datas: datas, province: province, city: city, style: .city, blockProvince: nil, blockCity: blockCity, blockArea: nil, blockTown: nil)
    }
    ///方法三Model：1.显示省份和市级和区域 2.传入了省份和城市和区域自动滚动到选中的，没有传或者找不到默认选中第一个 3.回调省份城市和区域
    @objc public class func areaModelPickerViewWith(datas: [MDHAddressPickerViewModel] = [MDHAddressPickerViewModel](), province: MDHAddressPickerViewModel = MDHAddressPickerViewModel(), city: MDHAddressPickerViewModel = MDHAddressPickerViewModel(), area: MDHAddressPickerViewModel = MDHAddressPickerViewModel(), blockArea: ((_ province: MDHAddressPickerViewModel, _ city: MDHAddressPickerViewModel, _ area: MDHAddressPickerViewModel)->())?) -> MDHAddressPickerView {
        return MDHAddressPickerView.addressModelPickerViewWith(datas: datas, province: province, city: city, area: area, style: .area, blockProvince: nil, blockCity: nil, blockArea: blockArea, blockTown: nil)
    }
    ///方法四Model：1.显示省份和市级和区域 镇 2.传入了省份和城市和区域自动滚动到选中的，没有传或者找不到默认选中第一个 3.回调省份城市和区域
    @objc public class func townModelPickerViewWith(datas: [MDHAddressPickerViewModel] = [MDHAddressPickerViewModel](), province: MDHAddressPickerViewModel = MDHAddressPickerViewModel(), city: MDHAddressPickerViewModel = MDHAddressPickerViewModel(), area: MDHAddressPickerViewModel = MDHAddressPickerViewModel(), town: MDHAddressPickerViewModel = MDHAddressPickerViewModel(), blockTown: ((_ province: MDHAddressPickerViewModel, _ city: MDHAddressPickerViewModel, _ area: MDHAddressPickerViewModel, _ town: MDHAddressPickerViewModel)->())?) -> MDHAddressPickerView {
        return MDHAddressPickerView.addressModelPickerViewWith(datas: datas, province: province, city: city, area: area, town: town, style: .town, blockProvince: nil, blockCity: nil, blockArea: nil, blockTown: blockTown)
    }
    ///私有方法 创建视图
    fileprivate class func addressModelPickerViewWith(datas: [MDHAddressPickerViewModel] = [MDHAddressPickerViewModel](), province: MDHAddressPickerViewModel = MDHAddressPickerViewModel(), city: MDHAddressPickerViewModel = MDHAddressPickerViewModel(), area: MDHAddressPickerViewModel = MDHAddressPickerViewModel(), town: MDHAddressPickerViewModel = MDHAddressPickerViewModel(), style: MDHAddressPickerViewType = .area, blockProvince: ((_ province: MDHAddressPickerViewModel)->())?, blockCity: ((_ province: MDHAddressPickerViewModel, _ city: MDHAddressPickerViewModel)->())?, blockArea: ((_ province: MDHAddressPickerViewModel, _ city: MDHAddressPickerViewModel, _ area: MDHAddressPickerViewModel)->())?, blockTown: ((_ province: MDHAddressPickerViewModel, _ city: MDHAddressPickerViewModel, _ area: MDHAddressPickerViewModel, _ town: MDHAddressPickerViewModel)->())?) -> MDHAddressPickerView {
        let _view = MDHAddressPickerView()
        
        _view.showType = style
        
        _view.selectProvinceModel = province
        _view.selectCityModel     = city
        _view.selectAreaModel     = area
        _view.selectTownModel     = area

        _view.provinceBlock  = blockProvince
        _view.cityBlock      = blockCity
        _view.areaBlock      = blockArea
        _view.townBlock      = blockTown
        
        ///获取数据
        if datas.count > 0 {
            _view.dataModels = datas
            _view.refreshData()
        }else{
            _view.setupData()
        }
        
        ///展示视图
        _view.showView()
        
        return _view
    }
    @objc convenience public init() {
        self.init(frame: UIScreen.main.bounds)
    }
    override public init(frame: CGRect) {
        super.init(frame: frame)
        ///
        self.rac_signal(for: UIControl.Event.touchUpInside)
            .deliverOnMainThread().subscribeNext { [weak self] (x) in
            if let weakSelf = self {
                weakSelf.hideView()
            }
        }
        ///创建视图
        setupView()
    }
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    ///刷新数据
    fileprivate func refreshData() -> () {
        
        var provinceIndex = 0
        var cityIndex = 0
        var areaIndex = 0
        var townIndex = 0
        
        if self.selectProvinceModel.name.count > 0 {
            for (p, itemP) in self.dataModels.enumerated() {
                ///省份
                if itemP.name == self.selectProvinceModel.name {
                    self.selectProvinceModel = itemP
                    provinceIndex = p
                    ///城市
                    if self.showType != .province && self.selectCityModel.name.count > 0 {
                        for (c, itemC) in itemP.items.enumerated() {
                            if itemC.name == self.selectCityModel.name {
                                self.selectCityModel = itemC
                                cityIndex = c
                                ///区域
                                if self.showType != .city && self.selectAreaModel.name.count > 0 {
                                    for (a, itemA) in itemC.items.enumerated() {
                                        if itemA.name == self.selectAreaModel.name {
                                            self.selectAreaModel = itemA
                                            areaIndex = a
                                            
                                            ///城镇
                                            if self.showType != .area && self.selectTownModel.name.count > 0 {
                                                for (t, itemT) in itemA.items.enumerated() {
                                                    if itemT.name == self.selectTownModel.name {
                                                        self.selectTownModel = itemT
                                                        townIndex = t
                                                        break
                                                    }
                                                }
                                            }
                                            
                                            break
                                        }
                                    }
                                }
                                
                                break
                            }
                        }
                    }
                    break
                }
            }
        }else {
            if self.dataModels.count > 0 {
                self.selectProvinceModel = self.dataModels[0]
                if self.selectProvinceModel.items.count > 0 {
                    self.selectCityModel = self.selectProvinceModel.items[0]
                    if self.selectCityModel.items.count > 0 {
                        self.selectAreaModel = self.selectCityModel.items[0]
                        if self.selectAreaModel.items.count > 0 {
                            self.selectTownModel = self.selectAreaModel.items[0]
                        }
                    }
                }
            }
        }
        
        let pickerView = self.pickerView ?? UIPickerView()
        pickerView.reloadAllComponents()
        self.pickerView?.selectRow(provinceIndex, inComponent: 0, animated: true)
        switch self.showType {
        case .province:
            break
        case .city:
            self.pickerView?.selectRow(cityIndex, inComponent: 1, animated: true)
            break
        case .area:
            self.pickerView?.selectRow(cityIndex, inComponent: 1, animated: true)
            self.pickerView?.selectRow(areaIndex, inComponent: 2, animated: true)
            break
        case .town:
            self.pickerView?.selectRow(cityIndex, inComponent: 1, animated: true)
            self.pickerView?.selectRow(areaIndex, inComponent: 2, animated: true)
            self.pickerView?.selectRow(townIndex, inComponent: 3, animated: true)
            break
        }

        /*
        let taiwans = ["屏东县",
                       "苗栗县",
                       "台东县",
                       "台中县",
                       "台北市",
                       "彰化县",
                       "高雄市",
                       "花莲县",
                       "南投县",
                       "基隆市",
                       "新竹市",
                       "澎湖县",
                       "云林县",
                       "台中市",
                       "嘉义市",
                       "嘉义县",
                       "台北县",
                       "台南县",
                       "宜兰县",
                       "桃园县",
                       "高雄县",
                       "新竹县"]
        let taiwan = self.dataModels[31]
        
        for (i, name) in taiwans.enumerated() {
            let item = MDHAddressPickerViewModel()
            item.name = name
            item.parentid = taiwan.id
            item.id = taiwan.id+String.init(format: "%02d", i+1)
            for j in 0..<1 {
                let itemA = MDHAddressPickerViewModel()
                itemA.name = name
                itemA.parentid = item.id
                itemA.id = item.id+String.init(format: "%02d", j+1)
                item.items.append(itemA)
                for k in 0..<1 {
                    let itemK = MDHAddressPickerViewModel()
                    itemK.name = name
                    itemK.parentid = itemA.id
                    itemK.id = itemA.id+String.init(format: "%03d", k+1)
                    itemA.items.append(itemK)
                }
            }
            taiwan.items.append(item)
        }
        print(taiwan.mj_JSONString())
        */
    }
    
    
    ///设置数据
    fileprivate func setupData() -> () {///mdh_pcat.geojson
        ///block 有数据时进行更新
        let blockRefresh = {
            [weak self] in
            if let weakSelf = self {
                weakSelf.dataModels = localPcaDatas
                weakSelf.refreshData()
            }
        }
        
        if localPcaDatas.count > 0 {
            blockRefresh()///更新数据
        }else{
            let data = Bundle.main.path(forResource: "mdh_p_c_a_t", ofType: "geojson") ?? ""//"mdh_p_city_area"
            if data.count <= 0 {
                return
            }
            if let json = try? String.init(contentsOfFile: data) {
                DispatchQueue.global().async {
                    let results = MDHAddressPickerViewModel.mj_objectArray(withKeyValuesArray: json) as? [MDHAddressPickerViewModel] ?? [MDHAddressPickerViewModel]()
                    localPcaDatas = results
                    DispatchQueue.main.async {
                        blockRefresh()///更新数据
                    }
                }
            }
        }
    }
    
    ///设置视图
    fileprivate func setupView() -> () {
        self.backgroundColor = UIColor.black.withAlphaComponent(0.01)
        self.frame = UIScreen.main.bounds
        let scale = kMDHPTAutoScale()
        let viewWidth = self.frame.width
        let bottomArea = kMMWPTFC().kSafeAreaBottom
        ///内容视图
        let viewContent = UIView.init(frame: CGRect.init(x: 0, y: self.frame.height, width: viewWidth, height: 270*scale+bottomArea))
        viewContent.backgroundColor = UIColor.white
        self.addSubview(viewContent)
        self.viewContain = viewContent
        viewContent.roundTop = true
        viewContent.dh_cornerRadius = 5
        
        ///顶部按钮 及 标题的背景视图
        let toolBar = UIView.init(frame: CGRect.init(x: 0, y: 0, width: viewWidth, height: 45*scale))
        viewContent.addSubview(toolBar)
        toolBar.backgroundColor = UIColor.color(hex: "#f6f6f6")
        toolBar.roundTop = true
        toolBar.dh_cornerRadius = 5
        
        ///左边的取消按钮
        let buttonCancel = MMWDHButton()
        toolBar.addSubview(buttonCancel)
        buttonCancel.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        buttonCancel.setTitle("取消", for: UIControl.State.normal)
        buttonCancel.setTitleColor(kColor_MDH666666(), for: UIControl.State.normal)
        buttonCancel.rac_signal(for: UIControl.Event.touchUpInside)
            .deliverOnMainThread().subscribeNext { [weak self] (x) in
                if let weakSelf = self {
                    weakSelf.hideView()
                }
        }
        let bcH = 44*scale
        buttonCancel.frame = CGRect.init(x: 5*scale, y: (toolBar.frame.height-bcH)/2.0, width: 56*scale, height: bcH)

        ///右边的确定按钮
        let buttonSure = MMWDHButton()
        toolBar.addSubview(buttonSure)
        buttonSure.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        buttonSure.setTitle("确定", for: UIControl.State.normal)
        buttonSure.setTitleColor(kColor_MDHTheme(), for: UIControl.State.normal)
        //buttonSure.contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.right
        buttonSure.rac_signal(for: UIControl.Event.touchUpInside)
            .deliverOnMainThread().subscribeNext { [weak self] (x) in
                if let weakSelf = self {
                    switch weakSelf.showType {
                    case .province:
                        if weakSelf.provinceBlock != nil {
                            weakSelf.provinceBlock!(weakSelf.selectProvinceModel)
                        }
                        break
                    case .city:
                        if weakSelf.cityBlock != nil {
                            weakSelf.cityBlock!(weakSelf.selectProvinceModel, weakSelf.selectCityModel)
                        }
                        break
                    case .area:
                        if weakSelf.areaBlock != nil {
                            weakSelf.areaBlock!(weakSelf.selectProvinceModel, weakSelf.selectCityModel, weakSelf.selectAreaModel)
                        }
                        break
                    case .town:
                        if weakSelf.townBlock != nil {
                            weakSelf.townBlock!(weakSelf.selectProvinceModel, weakSelf.selectCityModel, weakSelf.selectAreaModel, weakSelf.selectTownModel)
                        }
                        break
                    }
                    weakSelf.hideView()
                }
        }
        buttonSure.frame = buttonCancel.frame
        buttonSure.frame.origin.x = viewWidth-buttonSure.frame.width-buttonCancel.frame.origin.x
        
        ///设置标题
        let label = self.labelTitle
        toolBar.addSubview(label)
        let lx = buttonCancel.frame.maxX+5*scale
        label.frame = CGRect.init(x: lx, y: buttonCancel.frame.origin.y, width: viewWidth-lx*2, height: bcH)
        
        ///下面的主视图
        let picker = UIPickerView.init(frame: CGRect.init(x: 0, y: toolBar.frame.maxY, width: viewWidth, height: viewContent.frame.height-toolBar.frame.height-bottomArea))
        picker.backgroundColor = UIColor.white
        picker.delegate = self
        picker.dataSource = self
        viewContent.addSubview(picker)
        self.pickerView = picker
    }
    
    ///展示视图
    @objc open func showView() -> () {
        UIApplication.shared.keyWindow?.addSubview(self)
        UIView.animate(withDuration: 0.25) {
            self.backgroundColor = UIColor.black.withAlphaComponent(0.3)
            self.viewContain?.transform = CGAffineTransform.init(translationX: 0, y: -(self.viewContain?.frame.height ?? 0))
        }
    }
    ///隐藏视图
    fileprivate func hideView() -> () {
        UIView.animate(withDuration: 0.25, animations: {
            self.backgroundColor = UIColor.black.withAlphaComponent(0.01)
            self.viewContain?.transform = CGAffineTransform.identity
        }) { (finish) in
            self.removeFromSuperview()
        }
    }
}


///PickerView
extension MDHAddressPickerView: UIPickerViewDelegate, UIPickerViewDataSource {
    ///有多少栏
    open func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return self.columnCount
    }
    ///每一栏的数据个数
    open func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        var count = 0
        if component == 0 {
            count = self.dataModels.count
        }else if component == 1 {
            count = self.selectProvinceModel.items.count
        }else if component == 2 {
            count = self.selectCityModel.items.count
        }else{
            count = self.selectAreaModel.items.count
        }
        return count
    }
    ///显示数据View
    open func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let label = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: self.frame.width/CGFloat(self.columnCount), height: 30*kMDHPTAutoScale()))
        label.adjustsFontSizeToFitWidth = true
        label.textAlignment = .center
        if (component == 0) {
            label.text = self.dataModels[row].name
        }else if (component == 1){
            label.text = self.selectProvinceModel.items[row].name
        }else if (component == 2){
            label.text = self.selectCityModel.items[row].name
        }else{
            label.text = self.selectAreaModel.items[row].name
        }
        return label
    }
    ///返回a高度
    open func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 34*kMDHPTAutoScale()
    }
    ///点击事件
    open func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        /*
        self.selectProvinceModel = localPcaDatas[row]
        if self.showType != .province || component == 0 {
            if self.selectProvinceModel.items.count <= 0 {
                self.selectCityModel = MDHAddressPickerViewModel()
            }else{
                self.selectCityModel = self.selectProvinceModel.items[0]
            }
            self.pickerView?.reloadComponent(1)
            self.pickerView?.selectRow(0, inComponent: 1, animated: true)
            if self.showType != .city || component == 1 {
                if self.selectCityModel.items.count <= 0 {
                    self.selectAreaModel = MDHAddressPickerViewModel()
                }else{
                    self.selectAreaModel = self.selectCityModel.items[0]
                }
                self.pickerView?.reloadComponent(2)
                self.pickerView?.selectRow(0, inComponent: 2, animated: true)
                if self.showType != .area || component == 2 || component == 3 {
                    if self.selectAreaModel.items.count > 0 {
                        self.selectTownModel = self.selectAreaModel.items[0]
                    }else{
                        self.selectTownModel = MDHAddressPickerViewModel()
                    }
                    self.pickerView?.reloadComponent(3)
                    self.pickerView?.selectRow(0, inComponent: 3, animated: true)
                    if component == 3 {
                        self.selectTownModel = self.selectAreaModel.items[row]
                    }
                }
            }
        }
        */
        
        switch component {
        case 0:///省份
            self.selectProvinceModel = self.dataModels[row]
            if self.showType != .province {
                if self.selectProvinceModel.items.count <= 0 {
                    self.selectCityModel = MDHAddressPickerViewModel()
                }else{
                    self.selectCityModel = self.selectProvinceModel.items[0]
                }
                self.pickerView?.reloadComponent(1)
                self.pickerView?.selectRow(0, inComponent: 1, animated: true)
                if self.showType != .city {
                    if self.selectCityModel.items.count <= 0 {
                        self.selectAreaModel = MDHAddressPickerViewModel()
                    }else{
                        self.selectAreaModel = self.selectCityModel.items[0]
                    }
                    self.pickerView?.reloadComponent(2)
                    self.pickerView?.selectRow(0, inComponent: 2, animated: true)
                    if self.showType != .area {
                        if self.selectAreaModel.items.count > 0 {
                            self.selectTownModel = self.selectAreaModel.items[0]
                        }else{
                            self.selectTownModel = MDHAddressPickerViewModel()
                        }
                        self.pickerView?.reloadComponent(3)
                        self.pickerView?.selectRow(0, inComponent: 3, animated: true)
                    }
                }
            }
            break
        case 1:///城市
            self.selectCityModel = self.selectProvinceModel.items[row]
            if self.showType != .city {
                if self.selectCityModel.items.count <= 0 {
                    self.selectAreaModel = MDHAddressPickerViewModel()
                }else{
                    self.selectAreaModel = self.selectCityModel.items[0]
                }
                self.pickerView?.reloadComponent(2)
                self.pickerView?.selectRow(0, inComponent: 2, animated: true)
                if self.showType != .area {
                    if self.selectAreaModel.items.count > 0 {
                        self.selectTownModel = self.selectAreaModel.items[0]
                    }else{
                        self.selectTownModel = MDHAddressPickerViewModel()
                    }
                    self.pickerView?.reloadComponent(3)
                    self.pickerView?.selectRow(0, inComponent: 3, animated: true)
                }
            }
            break
        case 2:///区域
            self.selectAreaModel = self.selectCityModel.items[row]
            if self.showType != .area {
                if self.selectAreaModel.items.count > 0 {
                    self.selectTownModel = self.selectAreaModel.items[0]
                }else{
                    self.selectTownModel = MDHAddressPickerViewModel()
                }
                self.pickerView?.reloadComponent(3)
                self.pickerView?.selectRow(0, inComponent: 3, animated: true)
            }
            break
        case 3:///镇
            self.selectTownModel = self.selectAreaModel.items[row]
            break
        default:
            break
        }
    }
}



//MARK: - ==== model
open class MDHAddressPickerViewModel: NSObject {
    ///当前的id
    @objc public var id = ""
    ///父类的id 属于哪个
    @objc public var parentid = ""
    ///当前的名字
    @objc public var name = ""
    
    ///是否选中
    @objc public var isSelected = false
    ///当前是第几个
    
    ///子类的 数组
    @objc public var items = [MDHAddressPickerViewModel]()
    
    override public static func mj_objectClassInArray() -> [AnyHashable : Any]! {
        return ["items": "MDHAddressPickerViewModel"]
    }
    override open func mj_keyValuesDidFinishConvertingToObject() {
        self.items = MDHAddressPickerViewModel.mj_objectArray(withKeyValuesArray: self.items).copy() as! [MDHAddressPickerViewModel]
    }
}
